package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.mapper.StrategyCatalogMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    @Autowired
    private StrategyCatalogMapper mapper;

    @Override
    public PageInfo<StrategyCatalog> selectList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<StrategyCatalog> list = mapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public List<StrategyCatalog> selectAll() {

        return mapper.selectAll();
    }


    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        if (strategyCatalog.getSequence() == null){
            Integer max = mapper.getMaxSequence(strategyCatalog.getStrategyId());
            strategyCatalog.setSequence(max == null ? 1 : max + 1);
        }
        if (strategyCatalog.getId() == null){
            mapper.insert(strategyCatalog);
        }else {
            mapper.updateByPrimaryKey(strategyCatalog);
        }
    }

    @Override
    public List<StrategyCatalog> selectListByStrateId(Long strateId) {

        return mapper.selectListByStrategyId(strateId);
    }

    @Override
    public void updateStateById(Long id, boolean state) {
        mapper.updateStateById(id,state);
    }

    @Override
    public List<StrategyCatalog> selectByStrategyId(Long strategyId) {
        return mapper.selectListByStrategyId(strategyId);

    }
}
